package com.cloudxingkong.dianyinke.NewTeacher.Ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Adapter.MMAdpater;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.NewTeacher.bean.TeacherMsg;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MineMessageActivity extends AppCompatActivity {

    private ListView listView;
    private List<TeacherMsg.ListBean> datas;

    private MMAdpater mmAdpater;
    private String sessionID;
    private SharedPreferences sharedPreferences;
    private ProgressBar pbMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_messages);
        listView = (ListView) findViewById(R.id.mm_listview);
        pbMsg = (ProgressBar) findViewById(R.id.pb_message);
        datas=new ArrayList<>();
        mmAdpater=new MMAdpater(datas,this);
        listView.setAdapter(mmAdpater);
        sharedPreferences = getSharedPreferences("data_dyk", 0);
        sessionID = CookieHelper.getSessionID(sharedPreferences);
        loadData();
    }

    private void loadData() {
        pbMsg.setVisibility(View.VISIBLE);
        RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + ThisUri.MSG_QRY);
        params.addHeader("cookie",sessionID);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("test", "拉取老师消息 ： " + result);
                Message msg = handler.obtainMessage();
                msg.what = 1;
                msg.obj = result;
                handler.sendMessage(msg);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                pbMsg.setVisibility(View.GONE);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    String result= (String) msg.obj;
                    pbMsg.setVisibility(View.GONE);
                    Gson gson=new Gson();
                    if (result != null&&!result.equals("")) {
                        TeacherMsg teacherMsg = gson.fromJson(result, TeacherMsg.class);
                        if (teacherMsg.getList()!=null&&teacherMsg.getList().size()>0){
                            Collections.reverse(teacherMsg.getList());
                            datas.addAll(teacherMsg.getList());
                            mmAdpater.notifyDataSetChanged();
                        }else{
                            Toast.makeText(MineMessageActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                    break;
            }
        }
    };

    public void back(View view) {
        finish();
    }
}
